{itemplate 'public/header'}
<form action="./index.php?" class="form-horizontal form-filter" id="form1">
	{php echo tpl_form_filter_hidden('store/statistic/order');}
	<input type="hidden" name="days" value="{$days}"/>
	<div class="form-group">
		<label class="col-xs-12 col-sm-3 col-md-2 control-label">筛选时间</label>
		<div class="col-sm-9 col-xs-12 js-daterange" data-form="#form1">
			<div class="btn-group">
				<a href="{php echo ifilter_url('days:0');}" class="btn {if !$days}btn-primary{else}btn-default{/if}">今天</a>
				<a href="{php echo ifilter_url('days:7');}" class="btn {if $days == 7}btn-primary{else}btn-default{/if}">最近7天</a>
				<a href="{php echo ifilter_url('days:30');}" class="btn {if $days == 30}btn-primary{else}btn-default{/if}">最近30天</a>
				<a href="javascript:;" class="btn js-btn-custom {if $days == -1}btn-primary{else}btn-default{/if}">自定义</a>
			</div>
			<span class="btn-daterange js-btn-daterange {if $days != -1}hide{/if}">
				{php echo tpl_form_field_daterange('stat_day', array('start' => $starttime, 'end' => $endtime));}
			</span>
		</div>
	</div>
</form>
<div class="panel panel-stat">
	<div class="panel-heading">
		<h3>总览</h3>
	</div>
	<div class="panel-body">
		<div class="col-md-3">
			<div class="title">
				营业总额(元)
				<i class="fa fa-info-circle" data-toggle="popover" data-placement="top" data-content="车主通过销售商品获得的有效订单的总金额。（参考公式：流水=在线支付金额+线下支付金额+车主补贴+平台补贴）"></i>
			</div>
			<div class="num-wrapper">
				<a class="num" href="javascript:;">
					<span id="html-total-fee">￥--</span>
				</a>
			</div>
		</div>
		<div class="col-md-3">
			<div class="title">
				总收入
				<i class="fa fa-info-circle" data-toggle="popover" data-placement="top" data-content="车主通过销售商品获得的有效订单的总收入。（参考公式：总收入=在线支付金额+线下支付金额+平台补贴-车主优惠-平台服务佣金-平台拼车费）"></i>
			</div>
			<div class="num-wrapper">
				<a class="num" href="javascript:;">
					<span id="html-store-final-fee">￥--</span>
				</a>
			</div>
		</div>
		<div class="col-md-3">
			<div class="title">
				有效订单量
				<i class="fa fa-info-circle" data-toggle="popover" data-placement="top" data-content="有效订单量：交易成功且用户未退单的订单。客单价：每笔订单的平均交易金额。（公式：客单价=营业总额÷有效订单数）"></i>
			</div>
			<div class="num-wrapper">
				<a class="num" href="javascript:;">
					<span id="html-total-success-order">--</span>
				</a>
				<span class="info" id="html-avg-pre-order">--</span>
			</div>
		</div>
		<div class="col-md-3">
			<div class="title">
				无效订单量
				<i class="fa fa-info-circle" data-toggle="popover" data-placement="top" data-content="无效订单量：用户支付成功但最终被取消的订单。包括用户取消、车主取消和系统因超时未处理取消的订单。损失营业额约：根据被取消的订单估算出的车主未获得的营业总额。"></i>
			</div>
			<div class="num-wrapper">
				<a class="num" href="javascript:;">
					<span id="html-total-cancel-order">--</span>
				</a>
				<span class="info" id="html-total-cancel-fee">--</span>
			</div>
		</div>
	</div>
</div>
<div class="clearfix {if !$days}hide{/if}">
	<div class="panel panel-trend">
		<div class="panel-heading">
			<h3>趋势图</h3>
		</div>
		<div class="panel-body">
			<div id="chart-order-holder" style="width: 100%;height:400px;"></div>
		</div>
	</div>
</div>
<form action="" class="form-table form" method="post">
	<div class="panel panel-table">
		<div class="panel-body table-responsive">
			<table class="table table-bordered table-hover text-center" style="background: #fff">
				<thead class="navbar-inner">
				<tr style="height: 50px">
					<th colspan="3"><strong class="text-danger">对账单以天为单位,只统计已完成的订单</strong></th>
					<th colspan="2" class="text-center">应收金额</th>
					<th colspan="3" class="text-center">应付金额</th>
					<th colspan="2"></th>
				</tr>
				<tr>
					<th>账期</th>
					<th>已完成单数</th>
					<th>已取消单数</th>
					<th>用户支付</th>
					<th>补贴(平台)</th>
					<th>补贴(车主)</th>
					<th>平台服务费</th>
					<th>平台拼车费</th>
					<th>实际到账</th>
					<th style="width:150px; text-align:right;">详情</th>
				</tr>
				</thead>
				<tbody>
				{loop $records $record}
					<tr>
						<td><strong>{$record['stat_day']}</strong></td>
						<td><strong>{php echo intval($record['total_success_order']);}</strong></td>
						<td><strong>{php echo intval($cancel_records[$record['stat_day']]['total_cancel_order']);}</strong></td>
						<td><span class="text-success">+￥{$record['final_fee']}</span></td>
						<td><span class="text-success">+￥{$record['plateform_discount_fee']}</span></td>
						<td><span class="text-danger">-￥{$record['store_discount_fee']}</span></td>
						<td><span class="text-danger">-￥{$record['plateform_serve_fee']}</span></td>
						<td><span class="text-danger">-￥{$record['plateform_delivery_fee']}</span></td>
						<td><strong class="text-success">￥{$record['store_final_fee']}</strong></td>
						<td style="text-align:right;">
							<a href="{php echo iurl('store/finance/order/log', array('stat_day' => $record['stat_day']))}" class="btn btn-default btn-sm" title="查看订单" data-toggle="tooltip" data-placement="top" target="_blank">查看订单</a>
						</td>
					</tr>
				{/loop}
				</tbody>
			</table>
		</div>
	</div>
</form>
<script type="text/javascript">
irequire(['echarts'], function(echarts){
	var option = {
		title: {
			text: '营业趋势图'
		},
		tooltip : {
			trigger: 'axis'
		},
		legend: {
			data:[]
		},
		toolbox: {
			feature: {
				saveAsImage: {}
			}
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		},
		xAxis : [{
			type : 'category',
			boundaryGap : false,
			data :[1, 2, 3]
		}],
		yAxis : [
			{
				type : 'value'
			}
		],
		series : []
	};
	var myChart = echarts.init($('#chart-order-holder')[0]);
	myChart.setOption(option);
	myChart.showLoading();

	$.post(location.href, function(data){
		myChart.hideLoading();
		var result = $.parseJSON(data);
		option.legend.data = result.message.message.titles;
		var xAxis = {
			type : 'category',
			boundaryGap : false,
			data : result.message.message.days
		};
		option.xAxis = xAxis;
		$.each(result.message.message.fields, function(k, v){
			var serie = {
				name: result.message.message.titles[k],
				type: 'line',
				areaStyle: {normal: {}},
				data: result.message.message[v]
			};
			option.series.push(serie);
		});
		myChart.setOption(option);
		var stat = result.message.message.stat;
		$('#html-total-fee').html('￥' + stat.total_fee);
		$('#html-store-final-fee').html('￥' + stat.store_final_fee);
		$('#html-total-success-order').html(stat.total_success_order);
		$('#html-avg-pre-order').html('客单价:￥' + stat.avg_pre_order);
		$('#html-total-cancel-order').html(stat.total_cancel_order);
		$('#html-total-cancel-fee').html('损失营业额约:￥' + stat.total_cancel_fee);
	});
});
</script>
{itemplate 'public/footer'}
